
package com.canbot.u05.update;

import android.os.Environment;
import android.util.Log;

import org.apache.tools.zip.ZipFile;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author wenshao<szujobs@hotmail.com>
 */
public class ZipUtils {

        /**
         * 压缩文件
         *
         * @param folderPath
         * @param filePath
         * @param zipOut
         * @throws Exception
         */
        public static void zipFiles(String folderPath, String filePath,
                                    ZipOutputStream zipOut) throws Exception {
                if (zipOut == null) {
                        return;
                }
                File file = new File(folderPath + filePath);

                // 判断是不是文件
                if (file.isFile()) {
                        ZipEntry zipEntry = new ZipEntry(filePath);
                        FileInputStream inputStream = new FileInputStream(file);
                        zipOut.putNextEntry(zipEntry);

                        int len;
                        byte[] buffer = new byte[100000];

                        while ((len = inputStream.read(buffer)) != -1) {
                                zipOut.write(buffer, 0, len);
                        }
                        inputStream.close();
                        zipOut.closeEntry();
                }
                else {
                        // 文件夹的方式,获取文件夹下的子文件
                        String fileList[] = file.list();
                        // 如果没有子文件, 则添加进去即可
                        if (fileList.length <= 0) {
                                ZipEntry zipEntry = new ZipEntry(filePath + File.separator);
                                zipOut.putNextEntry(zipEntry);
                                zipOut.closeEntry();
                        }
                        // 如果有子文件, 遍历子文件
                        for (int i = 0; i < fileList.length; i++) {
                                zipFiles(folderPath, filePath + File.separator + fileList[i],
                                        zipOut);
                        }
                }
        }

        // 解压资源文件
        public boolean unZip(String zipfile, String targetdir, String tag) {
                // 开始解压资源文件
                String zipFile = Environment.getExternalStorageDirectory() + zipfile;
                String targetDir = Environment.getExternalStorageDirectory()
                        + targetdir;
                Log.e(tag, "source file:" + zipFile);
                Log.e(tag, "targetDir:" + targetDir);
                int BUFFER = 4096; // 这里缓冲区我们使用4KB，
                String strEntry; // 保存每个zip的条目名称

                try {
                        BufferedOutputStream dest = null; // 缓冲输出流
                        FileInputStream fis = new FileInputStream(zipFile);
                        ZipInputStream zis = new ZipInputStream(
                                new BufferedInputStream(fis));
                        ZipEntry entry; // 每个zip条目的实例
                        Log.e(tag, "zis is null?=" + zis.getNextEntry());
                        String ab = "11111111111111111111111111111";
                        while ((entry = zis.getNextEntry()) != null) {
                                try {
                                        Log.e(tag, "Unzip=" + entry);
                                        int count;
                                        byte data[] = new byte[BUFFER];
                                        strEntry = entry.getName();
                                        Log.e("Entry", entry.toString());
                                        Log.e("strEntry", strEntry);
                                        File entryFile = new File(targetDir + strEntry);
                                        File entryDir = new File(entryFile.getParent());
                                        if (!entryDir.exists()) {
                                                entryDir.mkdirs();
                                        }
                                        if (entryFile.exists()) {
                                                entryFile.delete();
                                        }
                                        entryFile.createNewFile();

                                        FileOutputStream fos = new FileOutputStream(entryFile);
                                        dest = new BufferedOutputStream(fos, BUFFER);
                                        while ((count = zis.read(data, 0, BUFFER)) != -1) {
                                                dest.write(data, 0, count);
                                        }
                                        dest.flush();
                                        dest.close();
                                }
                                catch (Exception ex) {
                                        ex.printStackTrace();
                                        return false;
                                }

                        }
                        zis.close();
                }
                catch (Exception cwj) {
                        cwj.printStackTrace();
                        return false;
                }
                Log.i(tag, "Unzip done");
                return true;

        }


        /**
         * 解压到指定目录
         *
         * @param zipPath
         * @param descDir
         * @author isea533
         */
        public static boolean unZipFiles(String zipPath, String descDir)
                throws IOException {
                return unZipFiles(new File(zipPath), descDir);
        }

        /**
         * 解压文件到指定目录
         *
         * @param zipFile
         * @param descDir
         * @author isea533
         */
        @SuppressWarnings("rawtypes")
        public static boolean unZipFiles(File zipFile, String descDir)
                throws IOException {
                File pathFile = new File(descDir);
                if (!pathFile.exists()) {
                        pathFile.mkdirs();
                }

                ZipFile zip = new ZipFile(zipFile, "GBK");
                for (Enumeration entries = zip.getEntries(); entries
                        .hasMoreElements(); ) {
                        org.apache.tools.zip.ZipEntry entry = (org.apache.tools.zip.ZipEntry) entries
                                .nextElement();
                        String zipEntryName = entry.getName();
                        System.out.println(zipEntryName);
                        InputStream in = zip.getInputStream(entry);
                        String outPath = (descDir + zipEntryName).replace("\\", "/");
                        // 输出文件路径信息
                        System.out.println(outPath);
                        // 判断路径是否存在,不存在则创建文件路径
                        File file = new File(
                                outPath.substring(0, outPath.lastIndexOf('/')));
                        if (!file.exists()) {
                                file.mkdirs();
                        }
                        File outfile = new File(outPath);
                        // outfile.createNewFile();
                        // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
                        if (outfile.isDirectory()) {
                                if (!outfile.exists()) {
                                        outfile.mkdir();
                                }
                                continue;
                        }
                        else {
                                outfile.createNewFile();
                        }

                        OutputStream out = new FileOutputStream(outfile);
                        byte[] buf1 = new byte[1024];
                        int len;
                        while ((len = in.read(buf1)) > 0) {
                                out.write(buf1, 0, len);
                        }
                        in.close();
                        out.close();
                }

                return true;
        }

}
